package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.user;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.services.UserService;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Renato Miceli
 */
public class RemoveUserButtonListener extends ButtonListenerAdapter {

	private final InfoManager<UserInfo> infoManager;

	private final TabPanel centerPanel;

	private final InfoManager<UserInfo> loggedInfoManager;

	public RemoveUserButtonListener(TabPanel centerPanel, InfoManager<UserInfo> infoManager,
			InfoManager<UserInfo> logedInfoManager) {
		this.centerPanel = centerPanel;
		this.infoManager = infoManager;
		this.loggedInfoManager = logedInfoManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(Button button, EventObject e) {
		MessageBox.confirm("Confirm", "Are you sure you want you want to remove this user?", new MessageBox.ConfirmCallback() {
			public void execute(String btnID) {
				if (btnID.equals("yes")) {
					UserInfo userToRemove = infoManager.getInfo();
					final UserInfo loggedInfo = loggedInfoManager.getInfo();
					UserService.Util.getInstance().removeUser(loggedInfo.getLogin(), loggedInfo.getPassword(), userToRemove.getLogin(), new RemoveUserCallback(centerPanel, infoManager));
				}
			}
		});	}
}
